//
//  AnimeOperation.m
//  iMAL
//
//  Created by Stefano Pigozzi on 2/9/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "AnimeOperation.h"
#import "NetworkOperation.h"

@implementation AnimeOperation

@synthesize xml;
@synthesize context;
@synthesize mal;

-(AnimeOperation *) initWithXML:(NSXMLElement *) initxml managedObjectContex:(NSManagedObjectContext *) initcontext malHandler: (MALHandler *) initmal
{
	self = [super init];
	if (self != nil) {
		self.xml = initxml;
		self.context = initcontext;
		self.mal = initmal;
	}
	return self;
}

-(void)main {

	// updating what we can update
	NSManagedObject * o = [context fetchOrCreateAnimeWithId: [[[[xml elementsForName:@"series_animedb_id"] objectAtIndex:0] stringValue] intValue]];
	[o setValue:[[[xml elementsForName:@"series_title"] objectAtIndex:0] stringValue] forKey:@"title"];
	[o setValue:[[[xml elementsForName:@"my_status"] objectAtIndex:0] stringValue] forKey:@"status"];
	[o setValue:[NSNumber numberWithInt:[[[[xml elementsForName:@"series_episodes"] objectAtIndex:0] stringValue] intValue]] forKey:@"episodes"];
	[o setValue:[NSNumber numberWithInt:[[[[xml elementsForName:@"my_watched_episodes"] objectAtIndex:0] stringValue] intValue]] forKey:@"watched_episodes"];
	
	// fetching page operation
	NSString * anime_url = [NSString stringWithFormat:@"%@/anime/%@", mal.url,[o valueForKey:@"anime_id"]];
	
	NSData *fetchedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:anime_url]]; 
	NSString * response_html = [[NSString alloc] initWithData:fetchedData encoding:NSUTF8StringEncoding];
	if(response_html == nil){ // backing off to ASCII encoding if UTF8 went wrong
		response_html = [[NSString alloc] initWithData:fetchedData encoding:NSASCIIStringEncoding];
	}
	NSString * title_alternatives = [response_html stringByMatching:@"Synonyms: ([^\"]+)\"" withReferenceFormat:@"$1"];
	[o setValue:title_alternatives forKey:@"title_alternatives"];
	
	NSString * image = [response_html stringByMatching:@"(http://cdn.myanimelist.net/images/anime/[0-9]+/[0-9]+.jpg)" withReferenceFormat:@"$1"];
	
	if([o valueForKey:@"image"]==nil){
		[o setValue:image forKey:@"image"];
		[[self.mal scheduledOperations] addOperation:[[NetworkOperation alloc] initWithUrl:image managedObjectContex:self.context malHandler:self.mal]];
	} else {
		NSNumber * newCurrent = [NSNumber numberWithInt:[[self.mal valueForKey:@"progressCurrent"] intValue]+1];
		[self.mal setValue:newCurrent forKey:@"progressCurrent"];
		[self.mal setValue:[NSString stringWithFormat:@"Skipping icon for: %@", [o valueForKey:@"title"]] forKey:@"progressMessage"];
	}
	
	NSNumber * newCurrent = [NSNumber numberWithInt:[[self.mal valueForKey:@"progressCurrent"] intValue]+1];
	[self.mal setValue:newCurrent forKey:@"progressCurrent"];
	[self.mal setValue:[NSString stringWithFormat:@"Importing: %@", [o valueForKey:@"title"]] forKey:@"progressMessage"];
	if([[self.mal progressCurrent] intValue] >= [[self.mal progressMaximum] intValue]){
		[self.mal hideProgressSheet];
	}
	[response_html release];
	return;
	
	
}

@end
